package com.gaogzhen.communication.waitNotify;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "w.PostureStep01")
public class PostureStep03 {
    static final Object room = new Object();
    // 是否又电
    static boolean hasElectric = false;
    // 是否有柴油
    static boolean hasDiesel = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (room) {
                log.debug("电有了没？[{}]", hasElectric);

                while (!hasElectric) {
                    log.debug("没电，先歇会！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("电接通了？[{}]", hasElectric);
                if (hasElectric) {
                    log.debug("开始干活！");
                }

            }
        },"李师傅").start();

        new Thread(() -> {
            synchronized (room) {
                log.debug("柴油送到没？[{}]", hasElectric);
                while (!hasDiesel) {
                    log.debug("柴油没送到，先歇会！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("柴油送到没？[{}]", hasElectric);
                if (hasDiesel) {
                    log.debug("开始干活！");
                }

            }
        },"王师傅").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            synchronized (room) {
                hasElectric = true;
                log.debug("电接通了！");
                room.notifyAll();
            }
        }, "工人").start();
    }
}
